package com.ttms.web.manage;

import com.ttms.pojo.Page;
import com.ttms.pojo.Studio;
import com.ttms.service.StudioService;
import com.ttms.service.impl.StudioServiceImpl;
import com.ttms.utils.WebUtils;
import com.ttms.web.common.BaseServlet;
import com.google.gson.Gson;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class StudioServlet extends BaseServlet {
    private StudioService studioService = new StudioServiceImpl();
    /**
     * 处理分页功能
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1 获取请求的参数 pageNo 和 pageSize
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //2 调用studioService.page(pageNo，pageSize)：Page对象
        Page<Studio> page = studioService.page(pageNo,pageSize);
        page.setUrl("/ttms/manage/studioServlet?action=page");
        //3 保存Page对象到Request域中
        req.setAttribute("page",page);
        //4 请求转发到.jsp页面
        req.getRequestDispatcher("/front-end/views/manage/studiomanage.jsp").forward(req,resp);
    }

    protected void pageByName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1 获取请求的参数 pageNo 和 pageSize
        System.out.println("hello");
        String studio_name = req.getParameter("studio_name");
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //2 调用studioService.page(pageNo，pageSize)：Page对象
        Page<Studio> page = studioService.queryStudioByName(pageNo,pageSize,studio_name);
        String url = "/ttms/manage/studioServlet?action=pageByName&studio_name=" +studio_name;
        page.setUrl(url);
        //3 保存Page对象到Request域中
        req.setAttribute("page",page);
        //4 请求转发到pages/manager/p_manager.jsp页面
        req.getRequestDispatcher("/front-end/views/manage/studiomanage.jsp").forward(req,resp);
    }

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Studio add = WebUtils.copyParamToBean(req.getParameterMap(),new Studio());
        studioService.addStudio(add);
        resp.sendRedirect("/ttms/manage/studioServlet?action=page&pageNo="+req.getParameter("pageNo"));
    }


    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        studioService.deleteStudioById(id);
        resp.sendRedirect(req.getContextPath() + "/manage/studioServlet?action=page&pageNo=" + req.getParameter("pageNo"));
    }



    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageNo = req.getParameter("pageNo");
        int id = WebUtils.parseInt(req.getParameter("studio_id"), 0);
        Studio update = WebUtils.copyParamToBean(req.getParameterMap(),new Studio());
        studioService.updateStudio(update);
        resp.sendRedirect("/ttms/manage/studioServlet?action=page&pageNo="+pageNo);
    }



    protected void getStudio(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1 获取请求的参数影片编号
        System.out.println(req.getParameter("id"));
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        System.out.println("查询的id为：" + id);
        //2 调用studioService.queryPlayById查询影片
        Studio studio = studioService.queryStudioById(id);
        studio.setStudio_id(id);
        System.out.println("====="+studio.getStudio_name());
        Gson gson = new Gson();
        String json = gson.toJson(studio);
        System.out.println(json);
        resp.getWriter().write(json);
    }

    protected void setSeat(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int studio_id = WebUtils.parseInt(req.getParameter("studio_id"), 0);
        System.out.println("studio_id为：" + studio_id);
        Studio studio = studioService.queryStudioById(studio_id);
        studioService.addSeat(studio_id);
        studio.setStudio_flag(1);
        studioService.updateStudio(studio);
        resp.sendRedirect(req.getContextPath() + "/manage/studioServlet?action=page&pageNo=" + req.getParameter("pageNo"));
    }

}
